<template>
  <div class="loginbody" v-wave="{
    color: 'currentColor',
    easing: 'ease-out',
    duration: 0.6,
    dissolveDuration: 0.25,
    initialOpacity: 0,

    finalOpacity: 0.1,
    cancellationPeriod: 75,
    trigger: 'auto',
    tagName: 'div'
  }">
    <div class="login-import-item link-type" @click.prevent="toHome">返回首页</div>
    <div class="loginmain">
      <div class="left">
        <img src="../assets/images/denglu_o.png" alt="">
      </div>
      <div class="right">
        <div class="tableall">
          <div class="login-table">
            <img src="../assets/images/denglu_logo.png" alt="">
            <p class="title-log">能力开放平台</p>
          </div>
          <div class="logbox">
            <div class="formbox">
              <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  
                <h3 class="title" style="font-size:20px;font-weight:500;color:black;">用户登录</h3>
                <el-form-item prop="username">
                  <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入用户名">
                    <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
                  </el-input>
                </el-form-item>
                <el-form-item prop="password">
                  <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="请输入密码"
                    @keyup.enter.native="handleLogin">
                    <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
                  </el-input>
  
                </el-form-item>
                <el-form-item prop="code" v-if="captchaEnabled">
                  <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
                    @keyup.enter.native="handleLogin">
                    <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
                  </el-input>
                  <div class="login-code">
                    <img :src="codeUrl" @click="getCode" class="login-code-img" />
                  </div>
                  <div style="float:right">
                    <p class="link-type" @click.prevent="retrieve">忘记密码？</p>
  
                  </div>
                </el-form-item>
  
                <!-- <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox> -->
  
  
                <el-form-item style="width:100%;">
                  <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
                    @click.native.prevent="handleLogin">
                    <span v-if="!loading" v-wave>登 录</span>
                    <span v-else>登 录 中...</span>
                  </el-button>
                  <div class="login-register" v-if="register">
                    <span>还没有账号？</span><router-link class="link-type" :to="'/register'">立即注册</router-link>
                  </div>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div :class="an">
      <div class="anbox1"></div>
      <div class="anbox2"></div>
      <div class="anbox3"></div>
      <div class="anbox4"></div>
      <div class="anbox5"></div>
      <div class="anbox6"></div>
      <div class="anbox7"></div>
      <div class="anbox8"></div>
    </div>

  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        an: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    retrieve() {
      // debugger
      // let routeData = this.$router.resolve({ path: '/retrieve'});
      //   window.open(routeData.href, '_blank');
      this.$router.push("/retrieve");

    },

    toHome() {
      this.$router.push("/home");
    },
    // gologin() {
    //   this.$router.push("/retrieve")
    // },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }

          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.an = "animation-bg"
            setTimeout(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(() => { });

            }, 1000);
            this.loading = false;

          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.loginbody {
  width: 100vw;
  height: 100%;
  background: linear-gradient(100deg, rgb(71, 186, 170), rgb(38, 178, 243), rgb(105, 198, 237));
  position: relative;
  min-width: 1500px;
  overflow: hidden;

  .animation-bg {
    position: absolute;
    top: 100vh;
    width: 100%;
    height: 100vh;
    display: flex;
    z-index: 999;

    .anbox1,
    .anbox2,
    .anbox3,
    .anbox4,
    .anbox5,
    .anbox6,
    .anbox7,
    .anbox8 {
      background: linear-gradient(180deg, rgb(71, 186, 170), rgb(0, 163, 141), rgb(0, 151, 161));

    }

    .anbox1 {
      width: 250px;
      height: 100vh;
      animation-name: anil1;
      animation-fill-mode: forwards;
      animation-duration: 0.2s;
    }

    @keyframes anil1 {
      to {
        transform: translateY(-100vh);

      }
    }

    .anbox2 {
      width: 250px;
      height: 100vh;

      animation-name: anil2;
      animation-fill-mode: forwards;
      animation-duration: 0.2s;
      animation-delay: 0.1s;

    }

    @keyframes anil2 {
      to {
        transform: translateY(-100vh);

      }
    }

    .anbox3 {
      width: 250px;
      height: 100vh;

      animation-name: anil3;
      animation-fill-mode: forwards;
      animation-duration: 0.2s;
      animation-delay: 0.2s;
    }

    @keyframes anil3 {
      to {
        transform: translateY(-100vh);

      }
    }

    .anbox4 {
      width: 250px;
      height: 100vh;

      animation-name: anil4;
      animation-fill-mode: forwards;
      animation-duration: 0.2s;
      animation-delay: 0.3s;
    }

    @keyframes anil4 {
      to {
        transform: translateY(-100vh);

      }
    }

    .anbox5 {
      width: 250px;
      height: 100vh;

      animation-name: anil5;
      animation-fill-mode: forwards;
      animation-duration: 0.2s;
      animation-delay: 0.4s;
    }

    @keyframes anil5 {
      to {
        transform: translateY(-100vh);

      }
    }

    .anbox6 {
      width: 250px;
      height: 100vh;

      animation-name: anil6;
      animation-fill-mode: forwards;
      animation-duration: 0.2s;
      animation-delay: 0.5s;
    }

    @keyframes anil6 {
      to {
        transform: translateY(-100vh);

      }
    }

    .anbox7 {
      width: 250px;
      height: 100vh;

      animation-name: anil7;
      animation-fill-mode: forwards;
      animation-duration: 0.2s;
      animation-delay: 0.6s;
    }

    @keyframes anil7 {
      to {
        transform: translateY(-100vh);

      }
    }

    .anbox8 {
      width: 250px;
      height: 100vh;

      animation-name: anil8;
      animation-fill-mode: forwards;
      animation-duration: 0.2s;
      animation-delay: 0.7s;
    }

    @keyframes anil8 {
      to {
        transform: translateY(-100vh);

      }
    }

  }

  .login-import-item {
    position: absolute;
    top: 2%;
    left: 2%;
    color: white;
    font-size: 16px;
    z-index: 33;
  }

  .logbox {
    position: relative;
    width: 405px;
    height: 436px;
    overflow: hidden;
  }

  .logbox::before {

    content: "";
    position: absolute;
    top: -50%;
    left: -50%;
    width: 400px;
    height: 430px;
    transform-origin: bottom right;
    animation: animate 6s linear infinite;
    background: linear-gradient(0deg, transparent, rgb(0, 255, 213), rgb(0, 255, 213));
  }

  .logbox::after {

    content: "";
    position: absolute; 
    top: -50%;
    left: -50%;
    width: 400px;
    height: 430px;
    transform-origin: bottom right;
    animation: animate 6s linear infinite;
    animation-delay: -3s;
    background: linear-gradient(0deg, transparent, rgba(0, 26, 255, 0.5), rgba(0, 26, 255, 0.5));
  }
  .formbox{
    position: absolute;
    inset: 3px;
    background:rgb(241, 247, 255);
    z-index:10;
  }

  @keyframes animate {
    0% {
      transform: rotate(0deg);
    }

    100% {
      transform: rotate(360deg);
    }
  }

  .loginmain {
    width: 100%;
    display: flex;
    height: 100%;

    .left {
      position: relative;
      width: 50%;
      height: 100%;

      img {
        position: absolute;
        width: 500px;
        height: 500px;
        top: calc(50% - 250px);
        left: calc(50% - 250px);
        min-width: 0px;
        min-height: 0px;
      }

    }

    .right {
      width: 50%;
      height: 100%;
      position: relative;
      // background: rgba($color: #ffffff, $alpha: 0.5);

      .tableall {
        position: absolute;
        top: calc(50% - 235px);
        left: calc(50% - 100px);
        z-index: 33;

        .login-table {
          width: 400px;
          justify-content: center;
          display: flex;
          height: 40px;
          align-items: center;
          margin-bottom: 10px;

          .title-log {
            color: white;
            opacity: 0.8;
            font-size: 20px;
            margin-left: 10px;
          }

          img {
            height: 40px;
            width: 40px;
          }
        }

        .login-form {
          border-radius: 6px;
          background: rgb(223, 243, 252);
          width: 400px;
          padding: 25px 25px 5px 25px;
          margin-bottom: 50px;
          height: 430px;

          .el-input {
            height: 38px;

            input {
              height: 38px;
            }
          }

          .input-icon {
            height: 39px;
            width: 14px;
            margin-left: 2px;
          }

          .title {
            margin: 30px auto 30px auto;
            text-align: center;
            font-weight: 500;
            font-size: 14px;
          }

          .login-register {
            text-align: center;
            margin-bottom: 10px;


            span {
              color: gray;
            }
          }
        }

      }


    }

  }

  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  .login-code {
    width: 33%;
    height: 38px;
    float: right;
  }

  .login-code-img {
    height: 38px;
  }

}
</style>
